<template>
  <div class="my-logs" :style="{ height: bodyH }">
    <div class="imgs">
      <!-- 图片 -->
      <img src="../../public/images/Group.png" />
    </div>
    <div class="item">
      <div class="itemImg">
        <img src="../../public/images/404/fbs.png" />
      </div>
      <!-- <div class="log-head">
        <router-link to="/reg"> 立即注册 </router-link>
      </div> -->
      <div class="log-input">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="userNameOrEmailAddress">
            <el-input
              v-model="loginForm.userNameOrEmailAddress"
              type="text"
              auto-complete="off"
              minlength="3"
              maxlength="12"
              prefix-icon="el-icon-user"
              placeholder="Username"
              style="margin-bottom: 2%"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              minlength="3"
              maxlength="12"
              clearable
              show-password
              prefix-icon="el-icon-unlock"
              placeholder="Password"
              @keyup.enter.native="handleLogin"
            >
            </el-input>
          </el-form-item>
          <!-- <el-checkbox
            v-model="loginForm.rememberMe"
            style="margin: 0px 0px 25px 0px"
            >记住密码</el-checkbox
          > -->
        </el-form>
      </div>
      <div class="button">
        <el-button
          icon="el-icon-right"
          size="medium"
          style="width: 60%"
          :loading="loading"
          @click.native.prevent="handleLogin"
          >登陆</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import { encrypt, decrypt } from "../util/jsencrypt";
export default {
  data() {
    return {
      loading: false,
      bodyH: 0,
      loginForm: {
        userNameOrEmailAddress: "",
        password: "",
        rememberMe: false,
      },
      loginRules: {
        userNameOrEmailAddress: [
          {
            required: true,
            trigger: "blur",
            message: "请输入您的账号或邮箱号",
          },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
      },
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    let h = document.documentElement.clientHeight || document.body.clientHeight;
    this.bodyH = h + "px";
  },
  methods: {
    //保存cookie
    getCookie() {
      const userNameOrEmailAddress = Cookies.get("userNameOrEmailAddress");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        userNameOrEmailAddress:
          userNameOrEmailAddress === undefined
            ? this.loginForm.userNameOrEmailAddress
            : userNameOrEmailAddress,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set(
              "userNameOrEmailAddress",
              this.loginForm.userNameOrEmailAddress,
              { expires: 30 }
            );
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("userNameOrEmailAddress");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }

          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router
                .push({ path: this.redirect || "/Index" })
                .catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../../public/styles/login.scss";
</style>
